import pygame
from jingling import *




class Plane():
	# 初始化
	def __init__(self):
		self.NUM = 0
		# 初始化屏幕
		self.screen = pygame.display.set_mode((SCREEN_RECT.width,SCREEN_RECT.height))

		
		# 初始化时钟
		self.clock = pygame.time.Clock()
		# 飞机
		# 敌方飞机
		self.shoot = pygame.image.load('./img/shoot.png')
		
		enemy_rect = pygame.Rect(538,616,51,35)
		self.enemy1_img = self.shoot.subsurface(enemy_rect)

		self.enemy2_img = self.shoot.subsurface(pygame.Rect(2,0,67,91))
		
		
		# 定时器
		pygame.time.set_timer(ENEMY_EVENT,500)
		pygame.time.set_timer(ENEMY_EVENT1,5000)
		

		# 我方飞机
		hero_rect = pygame.Rect(0,102, 99, 128)
		self.hero_img = self.shoot.subsurface(hero_rect)

		# 子弹
		self.bullet_rect = pygame.Rect(1003,987,10,23)
		self.bullet_image = self.shoot.subsurface(self.bullet_rect)
		pygame.time.set_timer(FIRE_BULLET_EVENT, 200)

		self.is_in_end = False
		# 初始化精灵
		self.__new_sprite()
		# 存键
		self.offset = {pygame.K_LEFT:0, pygame.K_RIGHT:0, pygame.K_UP:0, pygame.K_DOWN:0}
		
		
		# 分数
		# self.is_in_end = False
		self.fenshu = 0
		pygame.font.init()
		self.font = pygame.font.SysFont(None,40)

		self.__music()

		# self.fenshu = 0
		# pygame.font.init()
		# self.font = pygame.font.SysFont(None,60)
	#背景音乐
	def __music(self):
		pygame.mixer.init()
		pygame.mixer.music.set_volume(0.4)
		pygame.mixer.music.load('./sound/game_music.mp3')
		pygame.mixer.music.play(-1)
	# 创建精灵组
	def __new_sprite(self):
		# 背景精灵
		bg1 = Background()
		bg2 = Background(is_two=True)
		self.bg_ground = pygame.sprite.Group(bg1,bg2)

		# 我方飞机精灵
		self.hero = Hero(self.hero_img)
		self.hero_group = pygame.sprite.Group(self.hero)

		# 敌机精灵
		self.enemy1 = Enemy(self.enemy1_img)
		# 大飞机
		self.enemy2 = BigEnemy(self.enemy2_img)
		

		self.enemies1_group = pygame.sprite.Group()
		self.enemies2_group = pygame.sprite.Group()
		


		# st = Startback()
		# self.st_group = pygame.sprite.Group(st)
		# 游戏结束
		gm = Gameover()
		self.gm_group = pygame.sprite.Group(gm)
	#游戏开始
	def start_game(self):
		print('游戏开始')
		
		while True:
			# 刷新频率
			self.clock.tick(60)
			# 时间监听
			self.__event_listen()
			# 飞机子弹
			self.__collied()
			# 跟新绘制精灵
			self.__update_sprite()
			# 积分
			self.text_surface = self.font.render(' fs :%s' % self.fenshu,True,(11,25,231))
			self.text_rect = (10,10)
			self.screen.blit(self.text_surface,self.text_rect)
			# 刷新屏幕
			pygame.display.update()
	# 监听
	def __event_listen(self):
		
		for event in pygame.event.get():
		# 监听敌机
			if event.type == pygame.QUIT:
				self.__gameover()

			if event.type == ENEMY_EVENT:
				self.enemy1 = Enemy(self.enemy1_img)
				self.enemies1_group.add(self.enemy1)
			if event.type == ENEMY_EVENT1:
				self.enemy2 = BigEnemy(self.enemy2_img)
				self.enemies2_group.add(self.enemy2)
			


		# 子弹
			if event.type == FIRE_BULLET_EVENT:
				self.hero.fire(self.bullet_image)
		# 监听我方
			if event.type == pygame.KEYDOWN:
				if event.key in self.offset:
					self.offset[event.key] = 5
					
			elif event.type == pygame.KEYUP:
				if event.key in self.offset:
					self.offset[event.key] = 0



		self.hero.speedx = ( self.offset[pygame.K_RIGHT] - self.offset[pygame.K_LEFT]  )
		self.hero.speedy = ( self.offset[pygame.K_DOWN] - self.offset[pygame.K_UP]  )
	# 飞机子弹碰撞
	def __collied(self):
		# 子弹和小敌机
		group_list1=pygame.sprite.groupcollide(self.hero.bullet_group,self.enemies1_group,True,True)
		if len(group_list1)>0:
			self.fenshu += 10

		# 子弹和大敌机
		
		for i in range(5):
			if pygame.sprite.groupcollide(self.hero.bullet_group,self.enemies2_group,True,False):
			
				self.NUM +=1
				# print(self.NUM)
			if self.NUM == 5:
				self.enemy2.kill()
				self.fenshu+=100
				self.NUM = 0
				


		# hero和敌机碰撞
		sprite_list1 = pygame.sprite.spritecollide(self.hero,self.enemies1_group,True)
		if len(sprite_list1) > 0:
			self.hero.kill()
			self.is_in_end = True
		# hero和大敌机碰撞
		sprite_list2 = pygame.sprite.spritecollide(self.hero,self.enemies2_group,True)
		if len(sprite_list2) > 0:
			self.hero.kill()
			self.is_in_end = True
	#跟新精灵组
	def __update_sprite(self):
		# 绘制背景
		if self.is_in_end==False:
			self.bg_ground.update()
			self.bg_ground.draw(self.screen)
			# 绘制我放飞机
			self.hero_group.update()
			self.hero_group.draw(self.screen)
			# 绘制敌方飞机
			self.enemies1_group.update()
			self.enemies1_group.draw(self.screen)
			# 敌方大飞机
			self.enemies2_group.update()
			self.enemies2_group.draw(self.screen)
			# 子弹
			self.hero.bullet_group.update()
			self.hero.bullet_group.draw(self.screen)
		
		elif self.is_in_end==True:
			self.gm_group.update()
			self.gm_group.draw(self.screen)

			self.text2_surface = self.font.render('fs:%s' % self.fenshu, True, (255, 255, 255))
			self.text2_rect = (190, 400)
			self.screen.blit(self.text2_surface, self.text2_rect)
	# 游戏结束
	def __gameover(self):
		# print('游戏结束\n得分:%d' % self.fenshu)
		pygame.quit()
		exit()
game = Plane()
game.start_game()